package com.manage.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.common.util.Examine;
import com.staff.entity.Staff;

@Entity
@Table(name="t_training_record")
public class TrainingRecord {       //培训记录实体类
	private Long id;
	private TrainingCourse trainingCourse;
	private Staff staff;
	private int day;
	private String content;
	
	
	
	//业务数据
	private Long  trainingCourseId;
	private String  trainingCourseName;
	private Long  staffId;
	private String  staffName;
	private Examine examine;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}	
	@ManyToOne(cascade=CascadeType.ALL)
	public TrainingCourse getTrainingCourse() {
		return trainingCourse;
	}	
	@ManyToOne(cascade=CascadeType.ALL)
	public Staff getStaff() {
		return staff;
	}
	public int getDay() {
		return day;
	}
	public String getContent() {
		return content;
	}
	@Transient
	public Long getTrainingCourseId() {
		return trainingCourseId;
	}
	@Transient
	public String getTrainingCourseName() {
		return trainingCourseName;
	}
	@Transient
	public Long getStaffId() {
		return staffId;
	}
	@Transient
	public String getStaffName() {
		return staffName;
	}
	@Transient
	public Examine getExamine() {
		return examine;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTrainingCourse(TrainingCourse trainingCourse) {
		this.trainingCourse = trainingCourse;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setExamine(Examine examine) {
		this.examine = examine;
	}
	public void setTrainingCourseId(Long trainingCourseId) {
		this.trainingCourseId = trainingCourseId;
	}
	public void setTrainingCourseName(String trainingCourseName) {
		this.trainingCourseName = trainingCourseName;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public static Specification<TrainingRecord> getSpecification( final TrainingRecord trainingRecord)
	{
		Specification<TrainingRecord> spec = new Specification<TrainingRecord>() {
			public Predicate toPredicate(Root<TrainingRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != trainingRecord && !StringUtils.isEmpty(trainingRecord.getTrainingCourse())) {
					 Predicate  p1 =  cb.equal(root.get("trainingCourse").as(TrainingCourse.class),trainingRecord.getTrainingCourse());
					 list.add(p1);
				 }
				 if(null != trainingRecord && !StringUtils.isEmpty(trainingRecord.getStaff())) {
					 Predicate  p2 =  cb.equal(root.get("staff").as(Staff.class), trainingRecord.getStaff());
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;	
	}
}
